package com.dsj.gxrd.common;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
 * 类名称: LongJsonSerializer
 * 类描述: 向前端返回时将Long转成字符串
*/
@Component
public class LongArrayJsonSerializer extends JsonSerializer<List<Long>> {
    @Override
    public void serialize(List<Long> value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        try {
            if(value!=null){
                List<String>list=new ArrayList<>(value.size());
                for (Long val : value) {
                    list.add(String.valueOf(val));
                }
                jsonGenerator.writeObject(list);
                return;
            }
        } catch (IOException e) {
        }
        jsonGenerator.writeString("");
    }
}
